<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="formData.id?'编辑':'新增'" :visible="isShowDialog" @close="btnCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addDept" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="user in employeeList"
            :key="user.id"
            :value="user.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDept, getDeptDetail, editDept } from '@/api/departments'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      employeeList: [],
      formData: {
        code: '',
        name: '',
        introduce: '',
        manager: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '必须在 2-10 字符之间', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '必须在 2-10 字符之间', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '必须在 2-50 字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    this.employeeList = await getEmployeeSimple()
  },
  methods: {
    async getDeptDetail(id) {
      this.formData = await getDeptDetail(id)
    },
    async btnOK() {
      try {
        // 校验表单
        await this.$refs.addDept.validate()
        // 发请求
        if (this.formData.id) {
        // 编辑
          await editDept(this.formData)
        } else {
        // 新增
          await addDept({
            ...this.formData,
            pid: this.nodeData.id
          })
        }
        // 提示用户
        this.$message.success('操作成功')
        // 更新页面数据
        this.$emit('reloadPage')
        // 关闭弹窗
        // this.isShowDialog = false
        this.$emit('closeDialog')
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      // 清理数据
      this.formData = {
        code: '',
        name: '',
        introduce: '',
        manager: ''
      }
      // 清理报错
      this.$refs.addDept.resetFields()
      // 关闭弹窗
      this.$emit('closeDialog')
    }
  }
}
</script>

<style>

</style>
